<?php
/**
 *      [迷你云] (C)2009-2012 南京恒为网络科技.
 *   软件仅供研究与学习使用，如需商用，请访问www.miniyun.cn获得授权
 * 
 */
?>
<?php

class UserBase extends MiniyunViewForm{
	public $avatar;	public $name;	public $nick;	public $email;	public $isAdmin;	public $disabled;	public $id;	public $phone;	public $space;	public $usedSpace;	public $fileCount;	public $officeCount;	public $imageCount;	public $musicCount;	public $vedioCount;	public function init(){
		$this->isAdmin = false;
	}

	
	public function load($userId){
		$user = User::model()->find("id=?",array($userId));
		if(isset($user)){
			$this->id = $userId;
			$this->name = $user["user_name"];
			$this->disabled = $user["user_status"]==0?false:true;
			$this->is_admin = false;
			$userMeta = new UserMeta();
			$metas = $userMeta->getUserBaseMate($userId);
			foreach($metas as $index=>$item){
				if($item->meta_key=="email"){
										$this->email = $item->meta_value;
				}
				if($item->meta_key=="avatar"){
										$this->avatar = $item->meta_value;
				}
				if($item->meta_key=="nick"){
										$this->nick = $item->meta_value;
				}
				if($item->meta_key=="is_admin"){
										$this->isAdmin = $item->meta_value=="1"?true:false;
				}
				if($item->meta_key=="phone"){
										$this->phone = $item->meta_value;
				}
				if($item->meta_key=="space"){
										$this->space = $item->meta_value;
				}
			}
						if(!isset($this->avatar)){
				$this->avatar=Yii::app()->params["defaultAvatar"];
			}
			if(!isset($this->email)){
				$this->email="";
			}
			if(!isset($this->phone)){
				$this->phone="";
			}
			if(!isset($this->isAdmin)){
				$this->isAdmin=false;
			}
			if(!isset($this->space)){
				$this->space=Yii::app()->params["app"]["defaultSpace"];
			}
			$this->space = $this->space*1024*1024;

		}
	}
	
	public function loadFull($userId){
		$this->load($userId);		$data = UserFile::model()->getUsedSpace($userId);
		$this->usedSpace   = 0;
		if(count($data)>0){
			$this->usedSpace   = $data[0]["usedSpace"];
		}
		$this->fileCount   = UserFile::model()->getCount($userId);
		$this->officeCount = UserFile::model()->getOfficeCount($userId);
		$this->imageCount  = UserFile::model()->getImageCount($userId);
		$this->musicCount  = UserFile::model()->getMusicCount($userId);
		$this->vedioCount  = UserFile::model()->getVedioCount($userId);
			
	}
	
	public function attributeLabels()
	{
		return array(
            'name'=>'登录名',
            'nick'=>'昵称',
            'email'=>'电子邮件',
            'phone'=>'联系电话',
            'role'=>'角色',
            'disabled'=>'可用',
            'use'=>'使用情况',
		);
	}
	
	public function getItemOperates(){
		$retVal = "<a href='".Yii::app()->createUrl("adminUserPanel/update")."&id=".$this->id."'>编辑</a>";
		if($this->disabled){
			$retVal = $retVal . "<a href='".Yii::app()->createUrl("adminUserPanel/enabled")."&id=".$this->id."'>| 解冻</a>";
		}else{
			if($this->isAdmin==false){
				$retVal = $retVal . "<a href='".Yii::app()->createUrl("adminUserPanel/disabled")."&id=".$this->id."'>| 冻结</a>";
			}
		}
		if($this->isAdmin==false){
			$retVal = $retVal . "<a href='".Yii::app()->createUrl("adminUserPanel/delete")."&id=".$this->id."'>| 删除</a>";
		}
		return $retVal;
	}

	
	public function nameDetail(){
		$name = $this->name;
		if(strlen($name)>13){
			$name = substr($name,0,10)."...";
		}
		return "<img src='".Yii::app()->request->baseUrl.$this->avatar."' style='width:32px'>".
		CHtml::link($this->name,Yii::app()->createUrl("adminUserPanel/show", array('id'=>$this->id)));
	}
	
	public function roleDetail(){
		if($this->isAdmin){
			return "<img src='".Yii::app()->request->baseUrl.Yii::app()->params["iconUserAdmin"]."' style='width:32px'>";
		}else{
			return "<img src='".Yii::app()->request->baseUrl.Yii::app()->params["iconUserNormal"]."' style='width:32px'>";
		}
	}
	
	public function disabledDetail(){
		if($this->disabled){
			return "<img src='".Yii::app()->request->baseUrl.Yii::app()->params["iconUserDisabled"]."' style='width:24px'>";
		}else{
			return "<img src='".Yii::app()->request->baseUrl.Yii::app()->params["iconUserEnabled"]."' style='width:24px'>";
		}
	}
	
	public function useDetail(){
		return $this->owner->Widget('zii.widgets.jui.CJuiProgressBar', array(
    		'value'=>(int)(($this->usedSpace/$this->space)*100),
    		'htmlOptions'=>array(
             'style'=>'width:100px;height:10px;',
		),
		));
	}
	
	public function serialize()
	{
		return array(
		"id"=>$this->id,
		"name"=>$this->name,
		"nick"=>$this->nick,
		"isAdmin"=>$this->isAdmin,
		);
	}
}